package com.yoona.sms.impl;

import com.github.qcloudsms.httpclient.HTTPException;
import com.yoona.common.enums.CommonEnums;
import com.yoona.common.response.BaseResponse;

import com.yoona.common.utils.MobileUtil;
import com.yoona.common.utils.RedisUtil;
import com.yoona.sms.SmsService;
import com.yoona.sms.SmsUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Email: m15602498163@163.com
 * @Author: yoonada
 * @Date: 2020/9/21
 * @Time: 8:50 下午
 * @Msg:
 */
@Slf4j
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class SmsServiceImpl implements SmsService {

    private final RedisUtil redisUtil;

    private static final String[] ADMIN_PHONE_NUMBER = new String[]{"15602498163","13046255574","15019758508","15016307414","13528390607","13229554266"};


    /**
     * @Author: YoonaDa
     * @Email: m15602498163@163.com
     * @CreateTime: 2020/10/29 5:47 下午
     * @Param: [mobilePhoneNumber]
     * @Return: com.yoona.common.response.BaseResponse<java.lang.Object>
     * @Description: 发送短信
     */
    @Override
    public BaseResponse<Object> sendMessage(String mobilePhoneNumber) {
        if (StringUtils.isAnyEmpty(mobilePhoneNumber)) {
            return BaseResponse.error(CommonEnums.MOBILE_PHONE_NUMBER_IS_EMPTY);
        }
        if (!MobileUtil.checkPhone(mobilePhoneNumber)){
            return BaseResponse.error(CommonEnums.MOBILE_PHONE_NUMBER_IS_INCORRECT);
        }
        boolean flag = Arrays.asList(ADMIN_PHONE_NUMBER).contains(mobilePhoneNumber);
        if (flag){
            /**
             * 判断redis中是否已经存在该手机号码对应的key,若存在,则先删除，再向redis添加
             */
            String a = redisUtil.get("smsVerificationCode" + ":" + mobilePhoneNumber);
            if (a != null) {
                redisUtil.del("smsVerificationCode" + ":" + mobilePhoneNumber);
                log.info("redis中存在该手机的验证码,已删除");
            }
            String verificationCode = SmsUtils.getCode();
            // 将验证码传入redis中
            redisUtil.set("smsVerificationCode" + ":" + mobilePhoneNumber, verificationCode, 600);
            log.info("已经将用户的验证码传入redis中");
            ArrayList<String> list = new ArrayList<>();
            // 验证码
            list.add(verificationCode);
            // 代表十分钟内失效
            list.add("10");
            try {
                SmsUtils.tencentSmsSend(mobilePhoneNumber, list);
            } catch (HTTPException | IOException e) {
                e.printStackTrace();
            }
            return BaseResponse.success(CommonEnums.SMS_SENT_SUCCESSFULLY);
        }
        return BaseResponse.error(CommonEnums.NOT_AN_ADMIN);

    }

    /**
     * @Author: YoonaDa
     * @Email: m15602498163@163.com
     * @CreateTime: 2020/10/29 5:46 下午
     * @Param: [mobilePhoneNumber, verificationCode]
     * @Return: com.yoona.common.response.BaseResponse<java.lang.Object>
     * @Description: 校验验证码
     */
    @Override
    public BaseResponse<Object> checkVerificationCode(String mobilePhoneNumber, String verificationCode) {
        if (StringUtils.isAnyEmpty(mobilePhoneNumber) || StringUtils.isAnyEmpty(verificationCode)) {
            return BaseResponse.error(CommonEnums.PARAM_IS_NULL);
        }
        String code = redisUtil.get("smsVerificationCode" + ":" + mobilePhoneNumber);
        if (!StringUtils.isAnyEmpty(code) && verificationCode.equals(code)) {
            return BaseResponse.success(CommonEnums.CHECK_VERIFICATION_CODE_SUCCESS);
        }
        return BaseResponse.success(CommonEnums.CHECK_VERIFICATION_CODE_FAILED);
    }
}
